//
//  RMMMyHuLiTableViewCell.m
//  Romomo
//
//  Created by puman on 2017/1/4.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMMyHuLiTableViewCell.h"

@implementation RMMMyHuLiTableViewCell
{
    UILabel *_promptLabel;
    UIImageView *_imageV;
    UILabel *_nameLabel;
    UILabel *_timeLabel;
    UILabel *_instructionsLabel;
    UILabel *_shenYuLabel;
    UIView *_view;
    UIView *_grayView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _selectBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _selectBut.frame = CGRectMake(0, 57.5, 45, 45);
        [_selectBut setImage:[UIImage imageNamed:@"可选@3x"] forState:(UIControlStateNormal)];
        [self.contentView addSubview:_selectBut];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(_selectBut.right, 10, kScreenWideth - _selectBut.right - 15, 140)];
        backView.layer.masksToBounds = YES;
        backView.layer.cornerRadius = 5;
        backView.layer.borderWidth = 1;
        backView.layer.borderColor = RGBA(239, 239, 239, 1).CGColor;
        backView.backgroundColor = kWhiteColor;
        [self.contentView addSubview:backView];
        
        _promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(backView.width - 66, 0, 66, 24)];
        _promptLabel.font = kFont(12);
        _promptLabel.text = @"即将到期";
        _promptLabel.hidden = YES;
        _promptLabel.textAlignment = NSTextAlignmentCenter;
        [backView addSubview:_promptLabel];
        
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 75, 75)];
        _imageV.layer.masksToBounds = YES;
        _imageV.layer.cornerRadius = 5;
        [backView addSubview:_imageV];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageV.right + 10, _imageV.top, backView.width - _imageV.right - 10 - _promptLabel.width, kMaric)];
        _nameLabel.font = kFont(16);
        [backView addSubview:_nameLabel];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageV.right + 10, _nameLabel.bottom + 15, backView.width - _imageV.right - 10, 20)];
        _timeLabel.font = kFont(12);
        [backView addSubview:_timeLabel];

        _instructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageV.right + 10, _timeLabel.bottom, backView.width - _imageV.right - 10 , 20)];
        _instructionsLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
        _instructionsLabel.font = kFont(12);
        [backView addSubview:_instructionsLabel];
        
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _imageV.bottom + 9.5, backView.width - 20, 0.5)];
        lineLabel.backgroundColor = RGBA(221, 221, 221, 1);
        [backView addSubview:lineLabel];
        
        _shenYuLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, lineLabel.bottom, 200, 45)];
        _shenYuLabel.textColor = kBasicColor;
        _shenYuLabel.font = kFont(14);
        [backView addSubview:_shenYuLabel];

        _view = [[UIView alloc] initWithFrame:CGRectMake(backView.width - 83, lineLabel.bottom + 10.5, 68, 24)];
        _view.layer.masksToBounds = YES;
        _view.layer.cornerRadius = 12;
        _view.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
        [backView addSubview:_view];
        
        _countLabel = [[UILabel alloc] initWithFrame:_view.bounds];
        _countLabel.font = kFont(13);
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.textColor = COLORFROMCODE(0x303137, 1);
        [_view addSubview:_countLabel];
        
        _subBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _subBut.frame = CGRectMake(_view.left - kRow, _view.top - kRow, 44, 44);
        [_subBut setImage:[UIImage imageNamed:@"un-subtract@3x"] forState:(UIControlStateNormal)];
        [backView addSubview:_subBut];
        
        _addBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _addBut.frame = CGRectMake(_subBut.right, _subBut.top, 44, 44);
        [_addBut setImage:[UIImage imageNamed:@"add@3x"] forState:(UIControlStateNormal)];
        [backView addSubview:_addBut];
        
    }
    return self;
}
- (void)setModel:(RMMHuLiModel *)model{
    [_imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseImageUrl,model.SER_URL]] placeholderImage:[UIImage imageNamed:@"加载中@3x"]];
    _countLabel.text = model.COUNT;
    _nameLabel.text = [RMMFileManager retrunObjWith:model.NAME];
    _timeLabel.text = [NSString stringWithFormat:@"有效期至:%@",model.END_DATE];
    _instructionsLabel.text = [NSString stringWithFormat:@"限%@使用",[RMMFileManager retrunObjWith:model.REGION]];
    [RMMFileManager changeLabelTextColorOneWithLabel:_instructionsLabel forRange:NSMakeRange(1, _instructionsLabel.text.length - 3) byColor:kBasicColor];
    _shenYuLabel.text = [RMMFileManager retrunObjWith:[NSString stringWithFormat:@"剩余%@次",model.AVA_QTY]];
    NSString *time = [RMMFileManager retrunObjWith:model.END_DATE];
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSDate *date2 = [NSDate dateWithTimeInterval:3600 * 24 * 30 sinceDate:date];
    NSString *dateString = [inputFormatter stringFromDate:date2];
    NSString *string = [inputFormatter stringFromDate:date];
    if ([model.AVA_QTY integerValue] == 0) {
        _promptLabel.text = @"已使用完";
        _promptLabel.backgroundColor = kBasicColor;
        _promptLabel.textColor = kWhiteColor;
        _promptLabel.hidden = NO;
        _addBut.userInteractionEnabled = NO;
        [_selectBut setImage:[UIImage imageNamed:@"不可选@3x"] forState:(UIControlStateNormal)];
        _selectBut.userInteractionEnabled = NO;
        _nameLabel.frame = CGRectMake(_imageV.right + 10, _imageV.top, kScreenWideth - _selectBut.right - 15 - _imageV.right - 10 - _promptLabel.width, kMaric);

    } else {
        if ([time compare:string] == NSOrderedAscending) {
            _promptLabel.text = @"已经到期";
            _promptLabel.hidden = NO;
            _addBut.userInteractionEnabled = NO;
            [_selectBut setImage:[UIImage imageNamed:@"不可选@3x"] forState:(UIControlStateNormal)];
            _selectBut.userInteractionEnabled = NO;
            _promptLabel.backgroundColor = RGBA(237, 238, 239, 1);
            _promptLabel.textColor = RGBA(212, 213, 214, 1);
            _nameLabel.frame = CGRectMake(_imageV.right + 10, _imageV.top, kScreenWideth - _selectBut.right - 15 - _imageV.right - 10 - _promptLabel.width, kMaric);
        } else {
            if ([model.isSelect isEqualToString:@"0"]) {
                [_selectBut setImage:[UIImage imageNamed:@"可选@2x"] forState:(UIControlStateNormal)];
            } else {
                [_selectBut setImage:[UIImage imageNamed:@"选中@3x"] forState:(UIControlStateNormal)];
            }
            if ([time compare:dateString] == NSOrderedAscending || [time compare:dateString] == NSOrderedSame) {
                _promptLabel.text = @"即将到期";
                _promptLabel.hidden = NO;
                _addBut.userInteractionEnabled = YES;
                _selectBut.userInteractionEnabled = YES;
                _promptLabel.backgroundColor = [UIColor redColor];
                _promptLabel.textColor = kWhiteColor;
                _nameLabel.frame = CGRectMake(_imageV.right + 10, _imageV.top, kScreenWideth - _selectBut.right - 15 - _imageV.right - 10 - _promptLabel.width, kMaric);
            } else {
                _promptLabel.hidden = YES;
                _selectBut.userInteractionEnabled = YES;
                _addBut.userInteractionEnabled = YES;
                _nameLabel.frame = CGRectMake(_imageV.right + 10, _imageV.top, kScreenWideth - _selectBut.right - 15 - _imageV.right - 10, kMaric);
            }
        }
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
